//
//  HMFindPassWordController.m
//  HeartMark
//
//  Created by rimi on 16/11/17.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMFindPassWordController.h"
#import <Masonry.h>
#import <AVOSCloud/AVOSCloud.h>
#import "HMAlertView.h"

@interface HMFindPassWordController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField* emailT;
@property(nonatomic,strong)UITextField* phoneT;
@property(nonatomic,strong)UIButton* emailResetBtn;
@end

@implementation HMFindPassWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:(15*16)/255.0 green:(15*16)/255.0 blue:(15*16)/255.0 alpha:1.0];
    //设置UI
    [self setupUI];
}
-(void)setupUI{
    UIView* navVC = [[UIView alloc]init];
    navVC.backgroundColor = APP_COLOR;
    [self.view addSubview:navVC];
    [navVC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.width.equalTo(@(SCREEN_WIDTH));
        make.height.equalTo(@64);
    }];
    //返回按钮
    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [navVC addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@8);
        make.top.equalTo(@28);
    }];
    
    //邮箱找回密码按钮
    UILabel* emailBtn = [[UILabel alloc] init];
    emailBtn.text = @"邮箱找回密码";
    emailBtn.textColor = [UIColor blackColor] ;
    [navVC addSubview:emailBtn];
    [emailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@20);
        make.left.equalTo(@(SCREEN_WIDTH*0.3));
        make.width.equalTo(@(SCREEN_WIDTH* 0.4));
        make.height.equalTo(@44);
    }];
    
    
    //邮箱文本
    UILabel* emailL = [[UILabel alloc]init];
    emailL.text = @"邮箱";
    emailL.font = [UIFont systemFontOfSize:17];
    emailL.textColor = [UIColor grayColor];
    [self.view addSubview:emailL];
    [emailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navVC.mas_bottom).offset(16);
        make.left.equalTo(@8);
    }];
    
//    邮箱输入文本
    _emailT = [[UITextField alloc]init];
    _emailT.placeholder = @"请输入您绑定的邮箱";
    [_emailT addTarget:self action:@selector(textChangeWithBtnChange) forControlEvents:UIControlEventEditingChanged];
    _emailT.font = [UIFont systemFontOfSize:15];
    _emailT.delegate = self;
    [self.view addSubview:_emailT];
    [_emailT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(emailL.mas_centerY);
        make.left.equalTo(emailL.mas_right).offset(30);
        make.width.equalTo(@(SCREEN_WIDTH*0.7));
        make.height.equalTo(@44);
    }];
    //分割灰线
    UIView* grayLineVc = [[UIView alloc]init];
    grayLineVc.backgroundColor = [UIColor grayColor];
    grayLineVc.alpha = 0.5;
    [self.view addSubview:grayLineVc];
    [grayLineVc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_emailT.mas_bottom).offset(2);
        make.left.equalTo(@8);
        make.right.equalTo(@-8);
        make.height.equalTo(@0.5);
    }];
    
    //重置密码按钮
    _emailResetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _emailResetBtn.layer.cornerRadius = 5;
    _emailResetBtn.backgroundColor = [UIColor orangeColor];
    [_emailResetBtn setTitle:@"重置密码" forState:UIControlStateNormal];
    [_emailResetBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _emailResetBtn.userInteractionEnabled = NO;
    [_emailResetBtn addTarget:self action:@selector(emailFindPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_emailResetBtn];
    [_emailResetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(grayLineVc.mas_bottom).offset(16);
        make.left.equalTo(grayLineVc.mas_left);
        make.right.equalTo(grayLineVc.mas_right);
        make.height.equalTo(@44);
    }];
}
//返回按钮回调
-(void)backBtnClicked{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//邮箱找回密码回调
-(void)emailFindPassword:(UIButton *)button{
    [button setUserInteractionEnabled:NO];
    NSString * emailText = self.emailT.text;
    [AVUser requestPasswordResetForEmailInBackground:emailText block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [HMAlertView bottomAlertView:@"发送找回密码邮件成功，请注意查看"];
        } else {
            [HMAlertView bottomAlertView:@"发送邮件失败，请检查邮箱是否正确"];
        }
        [button setUserInteractionEnabled:YES];
    }];
}



//提示框
-(void)setAlertVc:(NSString*)string{
    UIAlertController* alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alertVc animated:YES completion:nil];
    
}
//文本输入框发生改变导致按钮变化的方法
-(void)textChangeWithBtnChange{
    if (_emailT.text.length > 0) {
        _emailResetBtn.userInteractionEnabled = YES;
        [_emailResetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }

}


@end
